CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

  SDA_PIN       = 29
  SCL_PIN       = 28
  ACK           = 0
  NAK           = 1

obj

  'i2c   :       "Basic_I2C_Driver"  
  i2c   :       "new_i2c"
  d     :       "Parallax Serial Terminal"
     
  
pub go | i, system_status, battery_adc_reading

  'start the serial object for communications back to the PC's serial terminal
  d.start(115_200)              'start comms at 115_200 baud
  d.clear

  'Start the I2C object.  This doesnt do anything other than setup the I/O pins.
  i2c.StartI2C(SDA_PIN, SCL_PIN)
  
  d.str(string("Sending to Restep", 13))

  'main loop, do it forever (like doing "for(;;)" in C)
  repeat

    d.home

'=======================================================================================================================================
'=======================================================================================================================================

    'zero out the system_status variable

    i2c.SendStart
    i2c.TxByte($EA & !$01)      'Slave Address + Write command
    i2c.TxByte($50)             '<---- First byte to send
    i2c.TxByte($50)             '<---- Second byte to send

    'PauseMS(10)

    i2c.SendStart
    i2c.TxByte($EA | $01)      'Slave Address + Read command

    system_status := i2c.RxByte(ACK)     '<---- First byte read back
    system_status |= i2c.RxByte(NAK)<<8  '<---- Second byte read back, should be saved as the high byte, so shift it 8 bits left
                               
    i2c.SendStop


'=======================================================================================================================================


    d.str(string("Sent the command: 0x50 0x50 (system status)."))
    d.NewLine
    d.str(string("Value received: "))
    d.hex(system_status, 4)                             'display the system_status value received from the PSoC, 4 hex digits (16 bits)
    d.NewLine

    PauseMS(10)

'=======================================================================================================================================
'=======================================================================================================================================

    'zero out the battery_adc_reading variable
    battery_adc_reading := 0

    i2c.SendStart
    i2c.TxByte($EA & !$01)      'Slave Address + Write command
    i2c.TxByte($52)             '<---- First byte to send
    i2c.TxByte($52)             '<---- Second byte to send

    'PauseMS(10)

    i2c.SendStart
    i2c.TxByte($EA | $01)      'Slave Address + Read command

    battery_adc_reading := i2c.RxByte(ACK)     '<---- First byte read back
    battery_adc_reading |= i2c.RxByte(NAK)<<8  '<---- Second byte read back
                               
    i2c.SendStop

'=======================================================================================================================================
    'print the results of the operation
    
    d.str(string("Sent the command: 0x52 0x52 (battery ADC reading)."))
    d.NewLine
    d.str(string("Value received: "))
    d.hex(battery_adc_reading, 4)                       'display the ADC reading received from the PSoC, 4 hex digits (16 bits)
    
    d.NewLine
    d.NewLine
    
    PauseMS(250)                                        'wait an additional 1/4 second
'=======================================================================================================================================




pub PauseMS(Duration)
''  Pause execution for 'duration' milliseconds.
''  This routine is based on the set clock frequency.
''  
''  params:  Duration = number of milliseconds to delay                                                                                               
''  return:  none

  waitcnt((((clkfreq / 1_000) * Duration - 3932) #> 381) + cnt)